﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_Help_frm_SystemCode : System.Web.UI.Page
{
    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindSystemCode(dst, intPageIndex);
        }
    }

    public void UpDateSystemCode(object sender, EventArgs e)
    {
        ClsMenuCDAL cls_GetSystemCode = new ClsMenuCDAL();
        ImageButton ibtn = (ImageButton)sender;

        int intCommandName = Int32.Parse(ibtn.CommandName);
        int intItem = Int32.Parse(ibtn.CommandArgument);
        TextBox txt0 = (TextBox)this.dgrdResult.Items[intItem].FindControl("txt0");
        TextBox txt1 = (TextBox)this.dgrdResult.Items[intItem].FindControl("txt1");
        TextBox txt2 = (TextBox)this.dgrdResult.Items[intItem].FindControl("txt2");
        TextBox txt3 = (TextBox)this.dgrdResult.Items[intItem].FindControl("txt3");
        TextBox txt4 = (TextBox)this.dgrdResult.Items[intItem].FindControl("txt4");
        TextBox txt5 = (TextBox)this.dgrdResult.Items[intItem].FindControl("txt5");
        TextBox txt6 = (TextBox)this.dgrdResult.Items[intItem].FindControl("txt6");
        DropDownList drop0 = (DropDownList)this.dgrdResult.Items[intItem].FindControl("drop0");
        ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intItem].FindControl("ibtnRsCancel");
        ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intItem].FindControl("ibtnRsEdit");
        ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intItem].FindControl("ibtnRsServer");

        if (intCommandName == 0)//取消编辑
        {
            this.GetSystemCode();
        }
        else if (intCommandName == 1)//编辑
        {
            txt5.Visible = ibtnRsEdit.Visible = false;
            drop0.Visible = ibtnRsCancel.Visible = ibtnRsServer.Visible = true;
                     
           
        }
        else//保存编辑
        {
        }
    }

    private void GetSystemCode()
    {
        ClsMenuCDAL cls_GetSystemCode = new ClsMenuCDAL();

        DataSet dst = cls_GetSystemCode.GetSystemCode();

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            cls_Property.SetOrGetCorporationDataSet = dst;

            this.BindSystemCode(dst, 0);
        }
        else
        {
            this.dgrdResult.CurrentPageIndex = 0;
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();
            this.lblRsCounts.Text = "共0条记录";
            this.lblPageCounts.Text = "共0页";
            this.lblCurrPage.Text = "当前第0页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = false;
        }
    }

    private void BindSystemCode(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = true;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = true;

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
                this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                this.GetSystemCode();
            }
            else
            {
                this.lblJs.Text = "";//清空Js脚本
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void txtGo_TextChanged(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strIndex = this.txtGo.Text.Trim();
            this.txtGo.Text = String.Empty;

            if (strIndex != "")
            {
                try
                {
                    int intIndex = Int32.Parse(strIndex) - 1;

                    if (intIndex >= 0 && intIndex < this.dgrdResult.PageCount)
                    {
                        this.BindSystemCode(dst, intIndex);
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('请输入正确的页码数！')</script>";
                    }
                }
                catch
                {
                    this.lblJs.Text = "<script>alert('请输入数字类型的页码！')</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('请输入要跳转的页码！')</script>";
            }
        }
    }

    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        string strKey = this.txtKey.Text.Trim();

        if (strKey != "" && strKey != "查询关键字")
        {
            ClsMenuCDAL cls_GetSystemCode = new ClsMenuCDAL();

            DataSet dst = cls_GetSystemCode.SearchSystemCode(strKey);

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                ClsPropertyDAL cls_Property = new ClsPropertyDAL();
                cls_Property.SetOrGetCorporationDataSet = dst;

                this.BindSystemCode(dst, 0);
            }
            else
            {
                this.lblJs.Text = "<script>alert('查询结果为空！')</script>";
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('请输入搜索关键字！')</script>";
        }
    }

    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            TextBox txt5 = (TextBox)e.Item.FindControl("txt5");
            TextBox txt6 = (TextBox)e.Item.FindControl("txt6");
            DropDownList drop = (DropDownList)e.Item.FindControl("drop0");
            ImageButton ibtnRsServer = (ImageButton)e.Item.FindControl("ibtnRsServer");

            //if (txt6.ReadOnly)
            //{
            //    txt6.Attributes.Add("onkeydown", "if(event.keyCode == 13){document.getElementById('" + ibtnRsServer.ClientID + "').click();}");
            //}
           string  a;
            string strIndex = e.Item.Cells[1].Text.Trim();
            if (strIndex == "False") { strIndex = "0"; a = "1"; }
            else { strIndex = "1"; a = "0"; }
                  drop.SelectedIndex = Int32.Parse(a);
            if (strIndex == "0")
            {
                txt5.Text = "正常";
            }
            else if (strIndex == "1")
            {
                txt5.Text = "停用";
            }
            else if (strIndex == "2")
            {
                txt5.Text = "按月复位编码";
            }
            else
            {
                txt5.Text = "按日复位编码";
            }

        }
    }

    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        this.GetSystemCode();
    }
}
